package com.example.mobile1931605084chy0603homework;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private SurfaceView svMainVideo;
    private Button btnMainPlay;
    private SurfaceHolder holder;
    private MediaPlayer mediaPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //设置显示容器
        holder = svMainVideo.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.readboy);
                mediaPlayer.setDisplay(holder);
                mediaPlayer.start();
                btnMainPlay.setText("暂停");
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });
        btnMainPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
                mediaPlayer.pause();
                btnMainPlay.setText("播放");
            }else {
                mediaPlayer.start();
                btnMainPlay.setText("暂停");
            }
            }
        });
    }

    private void initView() {
        svMainVideo = (SurfaceView) findViewById(R.id.svMainVideo);
        btnMainPlay = (Button) findViewById(R.id.btnMainPlay);
    }
}